package com.lanlian.commonlib.network;

import com.lanlian.commonlib.common.SystemConfig;
import com.lanlian.volleylib.AbsHttpRequestProxy;
import com.lanlian.volleylib.RequestListener;
import com.lanlian.volleylib.annotation.HttpReqParam;

import java.util.TreeMap;

public class QCHttpRequestProxy extends AbsHttpRequestProxy {

    public QCHttpRequestProxy(Builder builder) {
        this.listener = builder.listener;
        this.requestParamBody = builder.requestParamBody;
        this.tag = builder.tag;
        this.cache = builder.cache;
        this.getCache = builder.getCache;
        HttpReqParam annotation = this.requestParamBody.getClass().getAnnotation(HttpReqParam.class);
        if (annotation == null) {
            this.protocol = builder.protocol;
            this.clazz = builder.clazz;
            this.method = HttpReqParam.HttpReqMethod.HTTP_POST;
        } else {
            this.protocol = annotation.protocol();
            this.clazz = annotation.responseType();
            this.method = annotation.method();
            this.expireTime = annotation.expireTime();
        }
    }

    public QCHttpRequestProxy() {
    }

    public static QCHttpRequestProxy.Builder get() {
        return new QCHttpRequestProxy.Builder();
    }

    @Override
    protected String getDomain() {
        return SystemConfig.getUserUrl();
    }

    @Override
    protected TreeMap<String, String> getCommonParamMap() {
        return null;
    }

    @Override
    protected TreeMap<String, String> getHeader() {
        TreeMap<String, String> heads = new TreeMap<>();
        heads.put("Connection", "Close");
        heads.put("Content-Type", "application/json;Charset=UTF-8");
        heads.put("Accept-Language", "zh,zh-cn");
        heads.put("Accept-Encoding", "gzip,deflate");
        return heads;
    }

    public static class Builder<T> {
        private RequestListener<T> listener;
        private Object requestParamBody;
        private String tag;
        private boolean cache;
        private boolean getCache;
        private Class clazz;
        private String protocol;

        public Builder() {
        }

        public Builder create(Object requestParamBody, RequestListener<T> listener) {
            this.listener = listener;
            this.requestParamBody = requestParamBody;
            return this;
        }

        public Builder tag(String tag) {
            this.tag = tag;
            return this;
        }

        public Builder protocol(String protocol) {
            this.protocol = protocol;
            return this;
        }

        public Builder clazz(Class clazz) {
            this.clazz = clazz;
            return this;
        }

        public Builder cache(boolean isCache) {
            this.cache = isCache;
            return this;
        }

        public Builder getCache(boolean getCache) {
            this.getCache = getCache;
            return this;
        }

        public QCHttpRequestProxy build() {
            return new QCHttpRequestProxy(this);
        }
    }

}
